import Vue from 'vue'
import App from './App.vue'
import router from './router'
// 引入elementUI
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI,{size: 'small'});
//引入Vuex
import store from "@/store";
//导入font-awesome
import 'font-awesome/css/font-awesome.css'

//全局引入请求方式
import {postRequest} from "@/utils/api";
import {putRequest} from "@/utils/api";
import {getRequest} from "@/utils/api";
import {deleteRequest} from "@/utils/api";
import {initMenu} from "@/utils/menus";

Vue.prototype.postRequest = postRequest
Vue.prototype.putRequest = putRequest
Vue.prototype.getRequest = getRequest
Vue.prototype.deleteRequest = deleteRequest

Vue.config.productionTip = false

//路由导航守卫
router.beforeEach((to, from, next) => {
  if (window.sessionStorage.getItem('tokenStr')) {
    initMenu(router,store)
    //判断是否存在用户信息
    if (!window.sessionStorage.getItem('user')) {
      return getRequest('/admin/info').then(resp => {
        if (resp) {
          window.sessionStorage.setItem('user',JSON.stringify(resp))
          next()
        }
      })
    }
    next()
  }else {
    //未登录的情况
    if (to.path === '/') {
      next()
    }else {
      next('/?redirect=' + to.path,true)
    }
  }
})


new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
